package com.onenine.chat.model;


import android.text.TextUtils;

import com.onenine.chat.commons.IConversation;
import com.onenine.chat.commons.IUser;
import com.onenine.chat.rongcloud.RcUser;

import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;

/**
 * 本地会话
 * 实现对融云的封装
 */
public class LConversation implements IConversation<LMessage> {

    private String targetId;
    private String portraitUrl;
    private String conversationTitle;
    private Conversation.ConversationType conversationType;
    private LMessage lastMessage;
    private int unreadCount;
    private long timeString;

    public LConversation(io.rong.imlib.model.Conversation conversation) {
        MessageContent messageContent = conversation.getLatestMessage();
        Message message = new Message();
        message.setContent(messageContent);
        message.setMessageId(conversation.getLatestMessageId());
        message.setSenderUserId(conversation.getSenderUserId());
        this.targetId = conversation.getTargetId();
        this.conversationTitle = conversation.getConversationTitle();
        this.conversationType = conversation.getConversationType();
        this.lastMessage = new LMessage(message);
        this.unreadCount = conversation.getUnreadMessageCount();
        this.timeString = conversation.getReceivedTime();
        if (conversation.getConversationType() == Conversation.ConversationType.PRIVATE) {
            //单聊
            final String meId = RcUser.getInstance().getUserId();
            if (TextUtils.equals(targetId, meId)) {
                this.targetId = conversation.getSenderUserId();
            }
            if (this.lastMessage != null) {
                IUser sendUser = this.lastMessage.getFromUser();
                if (sendUser != null
                        && TextUtils.equals(this.targetId, sendUser.getUserId())) {
                    this.conversationTitle = sendUser.getNickName();
                    this.portraitUrl = sendUser.getPhoto();
                }
            }
        } else {
            //群组或聊天室
            this.targetId = conversation.getTargetId();
            this.conversationTitle = conversation.getConversationTitle();
            this.portraitUrl = conversation.getPortraitUrl();
        }
    }

    @Override
    public String getTargetId() {
        return targetId == null ? "" : targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    @Override
    public String getPortraitUrl() {
        return portraitUrl == null ? "" : portraitUrl;
    }

    public void setPortraitUrl(String portraitUrl) {
        this.portraitUrl = portraitUrl;
    }

    @Override
    public String getConversationTitle() {
        return conversationTitle == null ? "" : conversationTitle;
    }

    @Override
    public Conversation.ConversationType getConversationType() {
        return conversationType;
    }

    public void setConversationTitle(String conversationTitle) {
        this.conversationTitle = conversationTitle;
    }

    @Override
    public LMessage getLastMessage() {
        return lastMessage;
    }

    @Override
    public void setLastMessage(LMessage lastMessage) {
        this.lastMessage = lastMessage;
    }

    @Override
    public int getUnreadCount() {
        return unreadCount;
    }

    @Override
    public long getTimestamp() {
        return timeString;
    }

    @Override
    public void clearUnreadCount() {
        unreadCount = 0;
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public String toString() {
        return "LConversation{" +
                "targetId=" + targetId +
                ", portraitUrl='" + portraitUrl + '\'' +
                ", conversationTitle='" + conversationTitle + '\'' +
                ", conversationType=" + conversationType +
                ", lastMessage=" + lastMessage +
                ", unreadCount=" + unreadCount +
                ", timeString=" + timeString +
                '}';
    }

}
